﻿layui.define(['element', 'layer', 'form'], function (exports) {
    var $ = layui.jquery;
    var element = layui.element;
    var layer = layui.layer;
    var form = layui.form;
    //监听左侧导航点击
    element.on('nav(leftNav)', function (elem) {
        var url = $(elem).children('a').attr('data-url');   //页面url
        var id = $(elem).children('a').attr('data-id');     //tab唯一Id
        var title = $(elem).children('a').text();           //菜单名称
        if (title === "首页") {
            element.tabChange('tab', 0);
            return;
        }
        if (url === undefined) return;
        var tabTitleDiv = $('.layui-tab[lay-filter="tab"]').children('.layui-tab-title');
        var exist = tabTitleDiv.find('li[lay-id=' + id + ']');
        if (exist.length > 0) {
            //切换到指定索引的卡片
            element.tabChange('tab', id);
        } else {
            var index = layer.load(1);
            setTimeout(function () {
                layer.close(index);
                var iframeElem = document.createElement('iframe');
                iframeElem.src = url;
                iframeElem.style = 'width:100%;height:100%;border:none;outline:none;';
                element.tabAdd('tab', { title: title, content:iframeElem.outerHTML, id: id });
                element.tabChange('tab', id);
            }, 100);

        }
    });

    //监听侧边导航开关
    form.on('switch(sidenav)', function (data) {
        if (data.elem.checked) {
            showSideNav();
            layer.msg('这个开关是layui的开关改编的');
        } else {
            hideSideNav();
        }
    });

    //收起侧边导航点击事件
    $('.layui-side-hide').click(function () {
        hideSideNav();
        $('input[lay-filter=sidenav]').siblings('.layui-form-switch').removeClass('layui-form-onswitch');
        $('input[lay-filter=sidenav]').prop("checked", false);
    });

    //鼠标靠左展开侧边导航
    $(document).mousemove(function (e) {
        if (e.pageX === 0) {
            showSideNav();
            $('input[lay-filter=sidenav]').siblings('.layui-form-switch').addClass('layui-form-onswitch');
            $('input[lay-filter=sidenav]').prop("checked", true);
        }
    });

    var isHide = false;
    //隐藏侧边导航
    function hideSideNav() {
        if (!isHide) {
            $('.layui-side').animate({ left: '-200px' });
            $('.layui-side-hide').animate({ left: '-200px' });
            $('.layui-body').animate({ left: '0px' });
            $('.layui-footer').animate({ left: '0px' });
            var tishi = layer.msg('鼠标靠左自动显示菜单', { time: 1500 });
            layer.style(tishi, {
                top: 'auto',
                bottom: '50px'
            });
            isHide = true;
        }
    }
    //显示侧边导航
    function showSideNav() {
        if (isHide) {
            $('.layui-side').animate({ left: '0px' });
            $('.layui-side-hide').animate({ left: '0px' });
            $('.layui-body').animate({ left: '200px' });
            $('.layui-footer').animate({ left: '200px' });
            isHide = false;
        }
    }
    exports('main', {});
});
